package com.quzhi.sys.service.impl;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.quzhi.sys.entity.OrderLog;
import com.quzhi.sys.entity.User;
import com.quzhi.sys.mapper.OrderLogMapper;
import com.quzhi.sys.service.IOrderLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xibing
 * @since 2024-08-06
 */
@Service
public class OrderLogServiceImpl extends ServiceImpl<OrderLogMapper, OrderLog> implements IOrderLogService {


    // 添加系统日志订单日志
    @Override
    public void AddSystemLog(String orderId, String log, String operator) {
        // 添加查重
        // 更具订单Id查询
        LambdaQueryWrapper<OrderLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderLog::getOrderId, orderId);
        List<OrderLog> orderLogs = this.baseMapper.selectList(wrapper);

        // 查询有重复就不添加
        for (OrderLog orderLog : orderLogs) {
            if (orderLog.getLog().equals(log)) return;
        }

        OrderLog orderLog = new OrderLog();
        orderLog.setId(UUID.randomUUID().toString());

        orderLog.setOrderId(orderId);
        orderLog.setLog(log);
        orderLog.setType(Boolean.FALSE);
        orderLog.setOperator(operator);

        orderLog.setCreateTime(LocalDateTime.now());

        this.baseMapper.insert(orderLog);
    }


    // 允许重复的系统日志
    @Override
    public void AddSystemLogExit(String orderId, String log, String operator) {
        // 添加查重

        OrderLog orderLog = new OrderLog();
        orderLog.setId(UUID.randomUUID().toString());

        orderLog.setOrderId(orderId);
        orderLog.setLog(log);
        orderLog.setType(Boolean.FALSE);
        orderLog.setOperator(operator);

        orderLog.setCreateTime(LocalDateTime.now());

        this.baseMapper.insert(orderLog);
    }


    @Override
    public void AddSystemLogCopyRawData(String orderId, String log, String operator, String RawData) {

        // 添加查重
        // 更具订单Id查询
        LambdaQueryWrapper<OrderLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderLog::getOrderId, orderId);
        List<OrderLog> orderLogs = this.baseMapper.selectList(wrapper);

        // 查询有重复就不添加
        for (OrderLog orderLog : orderLogs) {
            if (orderLog.getLog().equals(log)) return;
        }


        OrderLog orderLog = new OrderLog();
        orderLog.setId(UUID.randomUUID().toString());

        orderLog.setOrderId(orderId);
        orderLog.setLog(log);
        orderLog.setType(Boolean.FALSE);
        orderLog.setOperator(operator);

        orderLog.setCreateTime(LocalDateTime.now());
        orderLog.setRawData(RawData);

        this.baseMapper.insert(orderLog);
    }

    // 添加人工日志
    @Override
    public void AddUserLog(String orderId, String log, String operator) {

        // 添加查重
        // 更具订单Id查询
        LambdaQueryWrapper<OrderLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderLog::getOrderId, orderId);
        List<OrderLog> orderLogs = this.baseMapper.selectList(wrapper);

        // 查询有重复就不添加
        for (OrderLog orderLog : orderLogs) {
            if (orderLog.getLog().equals(log)) return;
        }


        OrderLog orderLog = new OrderLog();
        orderLog.setId(UUID.randomUUID().toString());

        orderLog.setOrderId(orderId);
        orderLog.setLog(log);
        orderLog.setType(Boolean.TRUE);
        orderLog.setOperator(operator);

        orderLog.setCreateTime(LocalDateTime.now());

        this.baseMapper.insert(orderLog);
    }
}
